package panda.server;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Collection;

import panda.parser.SQLLexer;
import panda.query.scan.Scanner;
import panda.query.struct.Attribute;
import panda.query.tree.QueryTreeNode;
import panda.syntax.SelectStm;
import panda.transaction.Transaction;

public class PandaMain {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws IOException {
		Panda.init(args[0]);

		String inpFN = args[1];
		String oupFN = "result.txt";

		BufferedReader rd = new BufferedReader(new FileReader(inpFN));
		BufferedWriter wr = new BufferedWriter(new FileWriter(oupFN));

		String line, input="";
		line=rd.readLine();

		while (line!=null) {
			input+=line;
			input+="\n";
			line=rd.readLine();
		}

		String[] inputSplit=input.split(";");

		Transaction tx = new Transaction();

		for (String sqlstatement : inputSplit) {
			SQLLexer lexer = new SQLLexer(new StringReader(sqlstatement));
			panda.parser.parser p = new panda.parser.parser(lexer);
			panda.parser.cup.runtime.Symbol symbol;
			
			try {
				symbol =p.parse();
			} catch (Exception e) {
				System.out.println(sqlstatement);
				continue;
			}
			Object statement=((ArrayList<Object>)symbol.value).iterator().next();
			if (statement instanceof SelectStm) {
				//					System.out.println("!!!");
				SelectStm select=((SelectStm) statement);
				QueryTreeNode tn;
				Scanner s;
				long time;
				int size;
				tn=Panda.getPlanner().executeQuery(select, tx);
//				System.out.println("BEGIN QUERY : ");
				ArrayList<String> selectExpr=PandaSelectGetColumn.getColumns(sqlstatement, select);
				Collection<Attribute> attrList=tn.getSchema().getAllAttributes();
				Attribute[] attrs = new Attribute[attrList.size()];
				
				for (Attribute a : attrList) attrs[tn.getSchema().IDof(a.getAttributeName()) - 1] = a;
				
				if (selectExpr.size() != 0) {
					size=selectExpr.size();
					for (int i=0; i<size; i++) {
						if (i<size-1)
							wr.append(selectExpr.get(i)+"\t");
						else wr.append(selectExpr.get(i) + "\n");
					}
				}
				else {
					
					size=attrList.size();
					for (int i=0; i<size; i++) {
						if (i<size-1)
							wr.append(attrs[i].getAttributeName()+"\t");
						else wr.append(attrs[i].getAttributeName() + "\n");
					}
				}
				s=tn.open();
				//					time = 0;
				//					time -= System.currentTimeMillis();
				while (s.next()) {
					for (int i=0; i<size; i++) {
						if (i<size-1)
							wr.append(s.getValue(attrs[i]).getContentValue()+"\t");
						else wr.append(s.getValue(attrs[i]).getContentValue() + "\n");
					}
				}
				wr.append("#\n");
				//					time += System.currentTimeMillis();
				s.close();
				//					System.out.println("TIME = " + time);
			}
			else {
				int nonsense=Panda.getPlanner().executeUpdate(statement, tx);
				//					tx.commit();
			}

			//			}
			//			catch (Exception e){
			// TODO Auto-generated catch block
			//				e.printStackTrace();
			//			}
		}

		tx.commit();
		rd.close();
		wr.close();
	}
}

